//
//  LHReadCollectController.m
//  EPA
//
//  Created by 卢浩 on 16/4/26.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHReadCollectController.h"
#import "LHReadCollectCell.h"
#import "LHDBManager.h"
#import "LHReadCollectDetailController.h"

@interface LHReadCollectController ()

@property (nonatomic,strong) NSMutableArray *readCollectArr;

@end

@implementation LHReadCollectController

- (NSMutableArray *)readCollectArr{
    if (_readCollectArr == nil) {
        _readCollectArr = [NSMutableArray array];
    }
    return _readCollectArr;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"个人收藏";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(backBarBtnAction)];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LHReadCollectCell" bundle:nil] forCellReuseIdentifier:@"LHReadCollectCell"];
}

- (void)loadData{
    NSArray *allReadCollectArr = [[LHDBManager shareManager] selectAllReadCollect];
    if (allReadCollectArr.count > 0) {
        [self.readCollectArr removeAllObjects];
        [self.readCollectArr addObjectsFromArray:allReadCollectArr];
        [self.tableView reloadData];
    }
}

//返回按钮事件
- (void)backBarBtnAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.readCollectArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LHReadCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHReadCollectCell" forIndexPath:indexPath];
    cell.model = self.readCollectArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LHReadCollectDetailController *readCollectDetailVC = [[LHReadCollectDetailController alloc]init];
    readCollectDetailVC.model = self.readCollectArr[indexPath.row];
    [self.navigationController pushViewController:readCollectDetailVC animated:YES];
}

@end
